package com.intelligent.bot.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.bot.api.TeamApi;
import com.intelligent.bot.base.exception.E;
import com.intelligent.bot.base.result.B;
import com.intelligent.bot.dao.ClassifyDao;
import com.intelligent.bot.dao.TeamClassifyDao;
import com.intelligent.bot.model.*;
import com.intelligent.bot.model.base.BaseDeleteEntity;
import com.intelligent.bot.model.req.*;
import com.intelligent.bot.model.res.ClassifyListRes;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = E.class)
public class ClassifyServiceImpl extends ServiceImpl<ClassifyDao, Classify> implements IClassifyService {

    @Resource
    IClassifyVideoService classifyVideoService;



    @Override
    public B<List<ClassifyListRes>> getList() {
        return B.okBuild(TeamApi.buildTree(baseMapper.getList()));
    }

    @Override
    public B<Void> add(ClassifyAddReq req) {
        Long count = this.lambdaQuery().eq(Classify::getClassifyName, req.getClassifyName()).count();
        if(count > 0){
            throw new E("分类名称已存在");
        }
        Classify classify = BeanUtil.copyProperties(req, Classify.class);
        this.save(classify);
        return B.okBuild();
    }

    @Override
    public B<Void> update(ClassifyUpdateReq req) {
        Long count = this.lambdaQuery().eq(Classify::getClassifyName, req.getClassifyName()).ne(Classify::getId, req.getId()).count();
        if(count > 0){
            throw new E("分类名称已存在");
        }
        Classify classify = BeanUtil.copyProperties(req, Classify.class);
        this.saveOrUpdate(classify);
        return B.okBuild();
    }

    @Override
    public B<Void> delete(BaseDeleteEntity req) {
        Long count = this.lambdaQuery().in(Classify::getParentId, req.getIds()).count();
        if(count > 0){
            throw new E("请先删除下级");
        }
        this.removeBatchByIds(req.getIds());
        return B.okBuild();
    }

    @Override
    public B<Void> allocation(ClassifyAllocationReq req) {
        if(!req.getVideoIds().isEmpty()){
            this.classifyVideoService.lambdaUpdate().eq(ClassifyVideo::getClassifyId, req.getClassifyId()).set(ClassifyVideo::getDeleted,1).update();
            List<ClassifyVideo> list = new ArrayList<>();
            req.getVideoIds().forEach(
                    c ->{
                        ClassifyVideo classifyVideo = new ClassifyVideo();
                        classifyVideo.setClassifyId(req.getClassifyId());
                        classifyVideo.setClassifyId(c);
                        list.add(classifyVideo);
                    }
            );
            this.classifyVideoService.saveBatch(list);
        }
        return B.okBuild();
    }

    @Override
    public B<List<Video>> getVideoList(ClassifyGetVideoListReq req) {
        return B.okBuild(classifyVideoService.getVideoListByClassifyId(req.getClassifyId()));
    }

    @Override
    public B<List<Video>> videoList(ClassifyGetVideoListReq req) {
        return B.okBuild(classifyVideoService.videoList(req.getClassifyId()));
    }
}
